layui.use(['table', 'admin', 'ax','element'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
	var element = layui.element
	
	//tab切换
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})

    /**
     * 计件明细管理
     */
    var ToolTaskMoney = {
        tableId: "toolTaskMoneyTable"
    };

    /**
     * 计件明细管理
     */
    var ToolTaskMoney2 = {
        tableId: "toolTaskMoneyTable2"
    };

    /**
     * 初始化表格的列
     */
    ToolTaskMoney.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'customerName', sort: true, title: '客户'},
            {field: 'v1', sort: true, title: '作业日期'},
            {field: 'v2', sort: true, title: '波次'},
            {field: 'v3', sort: true, title: '类型'},
            {field: 'arrangeNumber', sort: true, title: '小组编号'},
            {field: 'userName', sort: true, title: '人员'},
            {field: 'goodsNumber', sort: true, title: '商品组合'},
            {field: 'num', sort: true, title: '组合数量', totalRowText: '合计：'},
            {field: 'orderNum', sort: true, title: '单量', totalRow: true},
            {field: 'v4', sort: true, title: '作业数', totalRow: true},
            {field: 'money', sort: true, title: '金额', totalRow: true},
        ]];
    };

    /**
     * 初始化表格的列
     */
    ToolTaskMoney2.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'customerName', sort: true, title: '客户'},
            {field: 'v1', sort: true, title: '作业日期'},
            {field: 'userName', sort: true, title: '人员', totalRowText: '合计：'},
            {field: 'money', sort: true, title: '金额', totalRow: true},
            {field: 'orderNum', sort: true, title: '单量', totalRow: true},
            {field: 'skuNum3', sort: true, title: '3个sku以上单量', totalRow: true},
            {field: 'skuNum5', sort: true, title: '5个sku以上单量', totalRow: true},
            {field: 'cost', sort: true, title: '每单成本'},
            {field: 'v4', sort: true, title: '作业数', totalRow: true},
            {field: 'bcost', sort: true, title: '每包成本'},
            {field: 'dnum', sort: true, title: '每单包数'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolTaskMoney.search = function () {
        var queryData = {};
        queryData['arrangeNumber'] = $("#arrangeNumber").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['v2'] = $("#v2").val();
        queryData['condition'] = $("#condition").val();
        queryData['goodsNumber'] = $("#goodsNumber").val();
        queryData['num'] = $("#num").val();
        table.reload(ToolTaskMoney.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 点击查询按钮
     */
    ToolTaskMoney2.search = function () {
        var queryData = {};
        queryData['customerId'] = $("#customerId2").val();
        queryData['condition'] = $("#condition2").val();
        table.reload(ToolTaskMoney2.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    ToolTaskMoney.exportExcel = function () {
        var checkRows = table.checkStatus(ToolTaskMoney.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 导出excel按钮
     */
    ToolTaskMoney2.exportExcel = function () {
        var checkRows = table.checkStatus(ToolTaskMoney2.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult2.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolTaskMoney.tableId,
        url: Feng.ctxPath + '/toolTaskMoney/list?waveId='+Feng.getUrlParam("waveId"),
        page: true,
	    limit: 120,
	    totalRow: true,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
		limits:[10,20,30,40,50,60,120,240,360,480,3000,5000,8000,11000],
        cols: ToolTaskMoney.initColumn()
    });

    // 渲染表格
    var tableResult2 = table.render({
        elem: '#' + ToolTaskMoney2.tableId,
        url: Feng.ctxPath + '/toolTaskMoney/listsum?waveId='+Feng.getUrlParam("waveId"),
        page: true,
	    limit: 40,
	    totalRow: true,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
		limits:[10,20,30,40,50,60,120,240,360,480],
        cols: ToolTaskMoney2.initColumn()
    });

	//选择订单明细
	table.on('checkbox(' + ToolTaskMoney2.tableId + ')', function(obj){
		var money = 0
		var orderNum = 0
		var v4 = 0
		if(obj.checked){
			if(obj.type == 'all'){
				var data = table.checkStatus(ToolTaskMoney2.tableId).data
				for (var i = 0; i < data.length; i++) {
					orderNum = (parseFloat(orderNum)+parseFloat(data[i].orderNum)).toFixed(2)
					money = (parseFloat(money)+parseFloat(data[i].money)).toFixed(2)
					v4 = (parseFloat(v4)+parseFloat(data[i].v4)).toFixed(2)
				}
				$("#money").text(money)
				$("#orderNum").text(orderNum)
				$("#cost").text((parseFloat(money)/parseFloat(orderNum)).toFixed(3))
				$("#v4").text(v4)
				$("#bcost").text((parseFloat(money)/parseFloat(v4)).toFixed(3))
				$("#dnum").text((parseFloat(v4)/parseFloat(orderNum)).toFixed(3))
			} else if (obj.type == 'one'){
				var data = table.checkStatus(ToolTaskMoney2.tableId).data
				for (var i = 0; i < data.length; i++) {
					orderNum = (parseFloat(orderNum)+parseFloat(data[i].orderNum)).toFixed(2)
					money = (parseFloat(money)+parseFloat(data[i].money)).toFixed(2)
					v4 = (parseFloat(v4)+parseFloat(data[i].v4)).toFixed(2)
				}
				$("#money").text(money)
				$("#orderNum").text(orderNum)
				$("#cost").text((parseFloat(money)/parseFloat(orderNum)).toFixed(3))
				$("#v4").text(v4)
				$("#bcost").text((parseFloat(money)/parseFloat(v4)).toFixed(3))
				$("#dnum").text((parseFloat(v4)/parseFloat(orderNum)).toFixed(3))
			}
		} else {
			if(obj.type == 'all'){
				$("#orderNum").text("0")
				$("#money").text("0")
				$("#cost").text("0")
				$("#bcost").text("0")
				$("#v4").text("0")
				$("#dnum").text("0")
			} else if (obj.type == 'one'){
				var data = table.checkStatus(ToolTaskMoney2.tableId).data
				for (var i = 0; i < data.length; i++) {
					orderNum = (parseFloat(orderNum)+parseFloat(data[i].orderNum)).toFixed(2)
					money = (parseFloat(money)+parseFloat(data[i].money)).toFixed(2)
					v4 = (parseFloat(v4)+parseFloat(data[i].v4)).toFixed(2)
				}
				$("#money").text(money)
				$("#orderNum").text(orderNum)
				$("#v4").text(v4)
				if(orderNum != 0){
					$("#cost").text((parseFloat(money)/parseFloat(orderNum)).toFixed(3))
					$("#bcost").text((parseFloat(money)/parseFloat(v4)).toFixed(3))
					$("#dnum").text((parseFloat(v4)/parseFloat(orderNum)).toFixed(3))
				} else {
					$("#cost").text("0")
					$("#bcost").text("0")
					$("#dnum").text("0")
				}
			}
		}
	 });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolTaskMoney.search();
    });

    // 搜索按钮点击事件
    $('#btnSearch2').click(function () {
        ToolTaskMoney2.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolTaskMoney.exportExcel();
    });

    // 导出excel
    $('#btnExp2').click(function () {
        ToolTaskMoney2.exportExcel();
    });
    
    // 单击行事件
    table.on('row(' + ToolTaskMoney2.tableId + ')', function (obj) {
		$('div[lay-id="' + ToolTaskMoney2.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + ToolTaskMoney2.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
    // 单击行事件
    table.on('row(' + ToolTaskMoney.tableId + ')', function (obj) {
		$('div[lay-id="' + ToolTaskMoney.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + ToolTaskMoney.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
});
